
package com.insigma.business.qrcode.soldier.service.impl;


import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.soldier.dao.CommonDataDictDao;
import com.insigma.business.qrcode.soldier.dto.CommonDataDictBSearcher;
import com.insigma.business.qrcode.soldier.entity.CommonDataDictDO;
import com.insigma.business.qrcode.soldier.service.CommonDataDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service("commonDataDictService")
public class CommonDataDictServiceImpl implements CommonDataDictService {

    @Autowired(required = false)
    private CommonDataDictDao commonDataDictDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;


    @Override
    public CommonDataDictDO getByCode(String code) {
        return commonDataDictDao.getByCode(code);

    }

    @Override
    public List<CommonDataDictDO> search(final CommonDataDictBSearcher commonDataDictBSearcher) {
        return commonDataDictDao.search(commonDataDictBSearcher);
    }

    @Override
    public String getDictLable(String moduleName, String code) {
        CommonDataDictBSearcher commonDataDictBSearcher = new CommonDataDictBSearcher(1,1000);
        commonDataDictBSearcher.setModuleName(moduleName);
        List<CommonDataDictDO> list = this.search(commonDataDictBSearcher);

        if(CollectionUtils.isEmpty(list)){
            return StringUtils.EMPTY;
        }
        list = list.stream().filter(entity->StringUtils.equals(entity.getCode(),code)).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(list)){
            return StringUtils.EMPTY;
        }

        return list.get(0).getValue();
    }

    @Override
    public CommonDataDictDO get(final String moduleName){
        return commonDataDictDao.get(moduleName);
    }

    
    @Override
    public Integer insert(final CommonDataDictDO commonDataDictDO){
        commonDataDictDO.setId(sequenceGenerator.genKey("COMMON_DATA_DICT_ID"));
        return commonDataDictDao.insert(commonDataDictDO);
    }

    
    @Override
    public Integer update(final CommonDataDictDO commonDataDictDO){
        return commonDataDictDao.update(commonDataDictDO);
    }

    
    @Override
    public Integer delete(final String moduleName, final String modifierType, final String modifierId){
        return commonDataDictDao.delete(moduleName,modifierType,modifierId);
    }

    @Override
    public Integer append(String module, String code, String value) {
        CommonDataDictDO byCode = getByCode(code);
        if (byCode != null) {
            byCode.setValue(byCode.getValue() + "|" +value);
            return commonDataDictDao.update(byCode);

        }
        CommonDataDictDO dictDO = new CommonDataDictDO();
        dictDO.setModuleName(module);
        dictDO.setCode(code);
        dictDO.setValue(value);
        dictDO.setUsed(false);
        return commonDataDictDao.insert(dictDO);
    }

    @Override
    public Integer deleteByCode(String code, String modifierType, String modifierId) {
        return commonDataDictDao.deleteByCode(code,modifierType,modifierId);

    }

    @Override
    public Integer setUsed(String code) {
        return commonDataDictDao.setUsed(code);

    }

}
